/*******************************
 * class Products
 */
(function(){
    // Products类构造函数
    var Products = window.Products = function( subject,conf )
    {
        // 注册(观察者)
        if( subject ){
            subject.attach(this);
        }

        this.request = null;

        // 读取配置
        this.exeFile = conf.exeFile;
        this.view = conf.view;

        this.catOps = conf.categoryOps;
        this.prdOps = conf.productOps;
        this.cmtOps = conf.commentOps;
        this.imgOps = conf.imageOps;

        // 功能模块对象
        this.cat = new Categories( this.exeFile,this.catOps,null,this.prdOps ),
        this.prd = new ProductsInfo( this.exeFile,this.prdOps,this.catOps,{"评论":this.cmtOps,"图像":this.imgOps} );
        this.cmt = new Articles( this.exeFile,this.cmtOps,this.prdOps,null );
        this.img = new Images( this.exeFile,this.imgOps,this.prdOps,null );
    }

    Products.fn = Products.prototype =
    {
        listening : function( cmd )
        {
            this.request = cmd;
            if( this.view != this.request["view"] ){  return false;    }

            if( null != this.cat ){
                this.AddCategory();
                this.RemoveCategories();
                this.ModifyCategory();
                this.MoveCategories();
                this.GetCategoryDetails();
                this.GetCategoriesTree();
            }

            if( null != this.prd ){
                this.AddProduct();
                this.RemoveProducts();
                this.ModifyProduct();
                this.MoveProducts();
                this.GetProductDetails();
                this.GetProductsList();
            }

            if( null != this.cmt ){
                this.AddComment();
                this.RemoveComments();
                this.ModifyComment();
                this.MoveComments();
                this.GetCommentDetails();
                this.GetCommentsList();
            }

            if( null != this.img ){
                this.AddImage();
                this.RemoveImages();
                this.ModifyImage();
                this.MoveImages();
                this.GetImageDetails();
                this.GetImagesList();
            }
        },

        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////

        AddCategory : function()
        {
            if( !this.isHandle(this.catOps["add"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.cat.add( this.request,dispArea.content);
        },

        RemoveCategories : function()
        {
            if( !this.isHandle(this.catOps["remove"]) ){  return;  }
            this.cat.remove( this.request );
        },

        ModifyCategory : function()
        {
            if( !this.isHandle(this.catOps["modify"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.cat.modify( this.request,dispArea.content);
        },

        MoveCategories : function()
        {
            if( !this.isHandle(this.catOps["move"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.cat.move( this.request,dispArea.content);
        },

        GetCategoryDetails : function()
        {
            if( !this.isHandle(this.catOps["details"]) ){  return;  }
            var dispArea = Products.ui.init();
            //this.cat.get_breadcrumb( this.request,dispArea.content);
            this.cat.get_detail( this.request,dispArea.content);
        },

        GetCategoriesTree : function()
        {
            if( !this.isHandle(this.catOps["list"]) && !this.isHandle("GetCategories") ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Categories.ui.toolbar( this.request ));
            //this.cat.get_breadcrumb( this.request,dispArea.content);
            this.cat.get_list( this.request,dispArea.content);
        },

        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////

        AddProduct : function()
        {
            if( !this.isHandle(this.prdOps["add"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.prd.add( this.request,dispArea.content);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        RemoveProducts : function()
        {
            if( !this.isHandle(this.prdOps["remove"]) ){  return;  }
            this.prd.remove(this.request,this.prdOps);
        },

        ModifyProduct : function()
        {
            if( !this.isHandle(this.prdOps["modify"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(ProductsInfo.ui.toolbarInModify(this.view,this.prdOps,this.request["id"]));
            this.prd.modify( this.request,dispArea.content,this.prdOps);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        MoveProducts : function()
        {
            if( !this.isHandle(this.prdOps["move"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(ProductsInfo.ui.toolbarInModify(this.view,this.prdOps,this.request["id"]));
            this.prd.move( this.request,dispArea.content);
        },

        GetProductDetails : function()
        {
            if( !this.isHandle(this.prdOps["details"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(ProductsInfo.ui.toolbarInDetail(this.view,this.prdOps,this.request["id"]));
            this.prd.get_details( this.request,dispArea.content);
        },

        GetProductsList : function()
        {
            if( !this.isHandle(this.prdOps["list"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(ProductsInfo.ui.toolbarInList(this.view,this.prdOps,this.request["catId"]));
            this.prd.get_list( this.request,dispArea.content);
        },

        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////

        AddComment : function()
        {
            if( !this.isHandle(this.cmtOps["add"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.cmt.add( this.request,dispArea.content);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        RemoveComments : function()
        {
            if( !this.isHandle(this.cmtOps["remove"]) ){  return;  }
            this.cmt.remove(this.request,this.cmtOps);
        },

        ModifyComment : function()
        {
            if( !this.isHandle(this.cmtOps["modify"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInModify(this.view,this.cmtOps,this.request["id"]));
            this.cmt.modify( this.request,dispArea.content,this.cmtOps);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        MoveComments : function()
        {
            if( !this.isHandle(this.cmtOps["move"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInModify(this.view,this.cmtOps,this.request["id"]));
            this.cmt.move( this.request,dispArea.content);
        },

        GetCommentDetails : function()
        {
            if( !this.isHandle(this.cmtOps["details"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInDetail(this.view,this.cmtOps,this.request["id"]));
            this.cmt.get_details( this.request,dispArea.content);
        },

        GetCommentsList : function()
        {
            if( !this.isHandle(this.cmtOps["list"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInList(this.view,this.cmtOps,this.request["catId"]));
            this.cmt.get_list( this.request,dispArea.content);
        },

        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////

        AddImage : function()
        {
            if( !this.isHandle(this.imgOps["add"]) ){  return;  }
            var dispArea = Products.ui.init();
            this.img.add( this.request,dispArea.content);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        RemoveImages : function()
        {
            if( !this.isHandle(this.imgOps["remove"]) ){  return;  }
            this.img.remove(this.request,this.imgOps);
        },

        ModifyImage : function()
        {
            if( !this.isHandle(this.imgOps["modify"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInModify(this.view,this.imgOps,this.request["id"]));
            this.img.modify( this.request,dispArea.content,this.imgOps);
        },

        // 此函数无用,它的功能将从get_list函数处绑定调用
        MoveImages : function()
        {
            if( !this.isHandle(this.imgOps["move"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInModify(this.view,this.imgOps,this.request["id"]));
            this.img.move( this.request,dispArea.content);
        },

        GetImageDetails : function()
        {
            if( !this.isHandle(this.imgOps["details"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInDetail(this.view,this.imgOps,this.request["id"]));
            this.img.get_details( this.request,dispArea.content);
        },

        GetImagesList : function()
        {
            if( !this.isHandle(this.imgOps["list"]) ){  return;  }
            var dispArea = Products.ui.init();
            dispArea.content.append(Articles.ui.toolbarInList(this.view,this.imgOps,this.request["catId"]));
            this.img.get_list( this.request,dispArea.content);
        },

        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////

        isHandle : function( viewId )
        {
            if( viewId != this.request["op"] ){
                return false;
            }else{
                return true
            }
        }
    };


    // Ui
    Products.ui = {
        topToolbar: "",
        content: "",
        bottomToolbar: "",

        init : function(){
            this.topToolbar = App.ui.toolbarWrap.clone(true);
            this.content = App.ui.contentWrap.clone(true);
            this.bottomToolbar =this.topToolbar.clone(true);

            App.ui.content.empty()             //主显示区域
                  .append(this.topToolbar)     //工具显示区域
                  .append(this.content)        //内容显示区域
                  .append(this.bottomToolbar);

            this.topToolbar.append(App.ui.btnReturn.clone(true));       //填充顶部工具
            this.bottomToolbar.append(App.ui.btnReturn.clone(true));    //填充底部工具条

            return this;
        }
    };
})();
